<?php

namespace app\index\validate;

use think\Validate;

class BaseValidate extends Validate
{
    /**
     * 定义验证规则
     * 格式：'字段名'    =>    ['规则1','规则2'...]
     *
     * @var array
     */
    protected $rule = [
        'trade_name|商品名称' => ['require', 'max:19', 'nameFilter:违禁词'],
        'unit_price|商品价格' => 'require|between:6,50000',
        'unit_score|奖励积分' => 'require|between:0,25000'
    ];

    /**
     * 定义错误信息
     * 格式：'字段名.规则名'    =>    '错误信息'
     *
     * @var array
     */
    protected $message = [
        'trade_name.require' => '请填写商品名称',
        'trade_name.nameFilter' => '商品名称不能为"违禁词"',
        'unit_price.require' => '请填写商品价格',
        'unit_score.require' => '请填写奖励积分',
        'trade_name.max' => '商品名称过长',
        'unit_price.between' => '商品价格必须在6元到50000元之间',
        'unit_score.between' => '奖励积分必须要0到255000积分之间',
    ];

    protected $scene = [
        'add' => ['trade_name', 'unit_price', 'unit_score'],
        'update' => ['unit_price', 'unit_score'],
        'delete' => ['trade_name']
    ];

    protected function sceneFind()
    {
        return $this->only(['trade_name'])
            ->remove('trade_name', ['require', 'max'])
            ->append('trade_name', '违禁品|毒品|淫秽物品')
            ->message('trade_name', "不能搜索违禁品,毒品,淫秽物品等");
    }

    /**
     * 自定义验证方法
     * @param $trade_name
     * @param $requestField
     * @return bool
     */
    protected function nameFilter($trade_name, $requestField)
    {

        return $trade_name == $requestField ? false : true;
    }
}
